<?php
/**
 * 发送短信相关服务
 */

namespace App\Http\Controllers\Common;


use App\Http\Controllers\BaseController;
use App\Exceptions\BasicException;  //引入框架异常处理类
use App\Services\Tools\SmsService;
use Illuminate\Http\Request;        //引用http请求类
use App\Response\Response;
class SmsController extends BaseController
{

    public function getSmsCode(Request $request)
    {
        $type = $request->input('type');
        $mobile = $request->input('mobile');
        if(empty($mobile) || !preg_match("/^1[3456789]\d{9}$/", $mobile)){
            throw new BasicException(10010, '手机号格式错误');
        }
        if(!in_array($type,[1, 2, 3, 4, 5, 6, 7, 8])){
            throw new BasicException(10010, '参数错误');
        }
        
        //验证手机号码是否已经验证
        $result = app('model_user')->checkUserExist($mobile);
        
        if ($result == false && in_array($type, [2, 3, 7])) {
            throw new BasicException(10001, '手机号码不存在');
        } elseif ($result == true && in_array($type, [4])) {
            throw new BasicException(10001, '手机号码已存在');
        }
        $sms_code = mt_rand(1000,9999);
        // 短信内容
        switch ($type) {
            case 1: //注册短信
                if (!$result) {
                    $template_id = 'SMS_179855152';
                } else {
                    $template_id = 'SMS_179855154';
                }
                break;
            case 2: //找回密码
                $template_id = 'SMS_179855151';
                break;
            case 3: //绑定手机号
                $template_id = 'SMS_179855150';
                break;
            case 4: //绑定手机号
                $template_id = 'SMS_179855150';
                break;
            case 5: //设置二级密码
                $template_id = 'SMS_179855150';
                break;
            case 6: //绑定银行卡
                $template_id = 'SMS_179855150';
                break;
            case 7: //找回QQ账号
                $template_id = 'SMS_179855150';
                break;
        
            case 8: //注销账号
                $template_id = 'SMS_193237063';
                break;
            default:
                $template_id = 'SMS_179855150';
                break;
        }
       
        $send_res = SmsService::send($mobile, $type,['code'=>$sms_code],1,$template_id);
        // 保存短信信息
       /* $data['sms_content'] = $sms_content;
        $data['sms_code'] = $sms_code;
        $data['mobile'] = $mobile;
        $data['type'] = $msg;
        $data['send_status'] = $send_res?1:0;
        $data['send_response_msg'] =  '';
        $data['add_time'] = time();
        app('model_sms_message')->insert($data);
        */
        if ($send_res) {
            return Response::sendData('发送成功');
        } else {
            throw new BasicException(10001, '发送失败,请稍后再试');
        }
    }
    
    
    public function checkSmsCode(Request $request)
    {
        $mobile = $request->input('mobile', '');
        $smsCode = $request->input('code', '');
        $type = $request->input('type', '');
        $result = SmsService::verify($mobile,$smsCode,$type);
        if (!$result) {
            $errorMsg = SmsService::getErrorMsg();
            $msg = !empty($errorMsg['error_msg'])?$errorMsg['error_msg']:'短信验证码错误或已失效';
            throw new BasicException(20010,$msg);
        }
        return Response::sendData(1, '验证正确');
    }
}
